package org.kgduyjj.toolkit.console;

import org.kgduyjj.toolkit.util.ObjectUtils;
import org.kgduyjj.toolkit.util.StringUtils;

import com.alibaba.fastjson.JSON;

/**
 * 命令行打印工具
 * 
 * @author Kgduyjj
 *
 */
public class ConsolePrinter {
    /**
     * 以模板格式化
     * @param template
     * @param params
     * @return
     */
    protected static String format(String template, Object... params) {
        return StringUtils.format(template, params);
    }

    /**
     * 以字符串模板组装打印
     * 
     * @param template
     * @param params
     */
    protected static void print(String template, Object... params) {
        System.out.println(format(template, params));
    }

    /**
     * 打印字符串
     * 
     * @param text
     */
    protected static void print(String text) {
        print("{}", text);
    }

    /**
     * 打印对象Object
     * 
     * @param object
     */
    protected static void print(Object object) {
        print("{}", ObjectUtils.isNull(object) ? null : String.valueOf(object));
    }

    /**
     * 打印JSON对象
     * 
     * @param object
     */
    protected static void printJson(Object object) {
        print(asJson(object));
    }

    /**
     * 转换为JSON
     * 
     * @param object
     * @return
     */
    protected static String asJson(Object object) {
        return JSON.toJSONString(object);
    }

}
